package binary_tree_traversal;

import sun.reflect.generics.tree.Tree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 所有遍历均可用迭代（借助stack）或递归
 */
public class 二叉树遍历_144_94_145_102 {
}

class 二叉树的层序遍历_102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        traversal(root, 1, res);
        // bfs(root, res);
        return res;
    }

    public void traversal(TreeNode root, int deepth, List<List<Integer>> res) {
        if (root == null) return;
        if(res.size() < deepth){
            res.add(new ArrayList<>());
        }
        // deepth 为 1  对应res index  为0
        res.get(deepth - 1).add(root.val);
        traversal(root.left, deepth + 1, res);
        traversal(root.right, deepth + 1, res);
    }


    public void bfs(TreeNode root, List<List<Integer>> res) {
        if (root == null) return;
        Queue<TreeNode> stack = new ArrayDeque<>();
        stack.offer(root);
        while(!stack.isEmpty()){
            int len = stack.size();
            List<Integer> levelData = new ArrayList<>();
            while (len > 0) {
                TreeNode cur = stack.poll();
                levelData.add(cur.val);
                if (cur.left != null) {
                    stack.offer(cur.left);
                }
                if (cur.right != null) {
                    stack.offer(cur.right);
                }
                len --;
            }
            res.add(levelData);
        }
    }

}

class 二叉树的前序遍历_144 {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        traversal(res, root);
        return res;
    }

    public void traversal(List<Integer> res, TreeNode root) {
        if (root == null) return;
        // 中 左右
        res.add(root.val);
        traversal(res, root.left);
        traversal(res, root.right);
    }
}

class 二叉树的后序遍历_145 {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        traversal(root, res);
        return res;
    }

    public void traversal(TreeNode root, List<Integer> res) {
        if (root == null) return;
        traversal(root.left, res);
        traversal(root.right, res);
        res.add(root.val);
    }

}

class 二叉树的中序遍历_94 {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        traversal(root, res);
        return res;
    }

    public void traversal(TreeNode root, List<Integer> res) {
        if (root == null) return;
        traversal(root.left, res);
        res.add(root.val);
        traversal(root.right, res);
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}